/*
 * @Author: 生俊甫 1758142861@qq.com
 * @Date: 2024-06-04 17:52:51
 * @LastEditors: 生俊甫 1758142861@qq.com
 * @LastEditTime: 2024-06-06 15:44:18
 * @FilePath: /sjf/2024-project/2024_centos/pro_knowledge/websocket_test.cpp
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */
#include <iostream>
#include <fstream>
#include <unistd.h> // For getcwd
#include <cstring>  // For std::strerror
#include <websocketpp/config/asio_no_tls.hpp>
#include <websocketpp/server.hpp>
//25
typedef websocketpp::server<websocketpp::config::asio> websocket_sev;

// // 读取文件内容的辅助函数
// std::string read_file(const std::string &path) {
//     std::ifstream file(path);
//     if (file) {
//         return std::string((std::istreambuf_iterator<char>(file)),
//                            std::istreambuf_iterator<char>());
//     } else {
//         throw std::runtime_error("Unable to open file: " + path);
//     }
// }

// // 获取文件的 MIME 类型
// std::string get_mime_type(const std::string &path) {
//     if (path.ends_with(".html")) return "text/html";
//     if (path.ends_with(".css")) return "text/css";
//     if (path.ends_with(".js")) return "application/javascript";
//     if (path.ends_with(".png")) return "image/png";
//     if (path.ends_with(".jpg") || path.ends_with(".jpeg")) return "image/jpeg";
//     if (path.ends_with(".gif")) return "image/gif";
//     return "application/octet-stream";
// }

void http_callback(websocket_sev* wssrv, websocketpp::connection_hdl hdl)
{
    //简单的返回一个http页面
    std::cout << "connect http_html success!" << std::endl;
    websocket_sev::connection_ptr conn = wssrv->get_con_from_hdl(hdl);
    std::cout << "body:" << conn->get_request_body() << std::endl;
    websocketpp::http::parser::request req = conn->get_request();
    std::cout << "method:" << req.get_method() << std::endl;
    std::cout << "url:" << req.get_uri() << std::endl; 
    std::string body = "<html><body><h1>hello websocket</h1></body></html>";
    //std::string body = read_file("/home/sjf/2024-project/html_24/html/Index.html");
    conn->set_body(body);
    conn->append_header("Content-Type", "text/html;charset=utf-8");
    conn->set_status(websocketpp::http::status_code::ok);
}
void open_callback(websocket_sev* wssrv, websocketpp::connection_hdl hdl)
{
    std::cout << "connect open success!" << std::endl;
}
void close_callback(websocket_sev* wssrv, websocketpp::connection_hdl hdl)
{
    std::cout << "connect close success!" << std::endl;
}
void message_callback(websocket_sev* wssrv, websocketpp::connection_hdl hdl,websocket_sev::message_ptr msg)
{
    //get_payload()函数是message_buffer下的获取websocket请求中的payload数据类型
    std::cout << "wsmsg:" << msg->get_payload() << std::endl;
    //简答的回复收到的信息
    wssrv->send(hdl,msg->get_payload(),websocketpp::frame::opcode::text);
}
int main()
{
    //1.创建websocket下的server对象
    websocket_sev wssrv;
    //2.设置日志等级
    //设置成关闭，后序使用自己的日志函数
    wssrv.set_access_channels(websocketpp::log::alevel::none);
    //3.初始化asio调度器
    wssrv.init_asio();
    //启动地址重用
    wssrv.set_reuse_addr(true);
    //4.设置回调函数
    wssrv.set_http_handler(std::bind(&http_callback,&wssrv,std::placeholders::_1));
    wssrv.set_open_handler(std::bind(&open_callback,&wssrv,std::placeholders::_1));
    wssrv.set_close_handler(std::bind(&close_callback,&wssrv,std::placeholders::_1));
    wssrv.set_message_handler(std::bind(&message_callback,&wssrv,std::placeholders::_1,std::placeholders::_2));
    //5.设置监听端口
    wssrv.listen(8085);
    //6.获取新连接
    wssrv.start_accept();
    //7.启动服务器
    wssrv.run();
    return 0;
}